package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.domain.MemberAddr;
import com.powernode.model.Result;
import com.powernode.service.MemberAddrService;
import com.powernode.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员收货地址控制层
 */
@Api(tags = "会员收货地址接口管理")
@RequestMapping("p/address")
@RestController
public class MemberAddrController {

    @Autowired
    private MemberAddrService memberAddrService;

//    p/address/list
    @ApiOperation("查询会员收货地址列表")
    @GetMapping("list")
    public Result<List<MemberAddr>> loadMemberAddrList() {
        String openId = AuthUtils.getLoginUserOpenId();
        List<MemberAddr> list = memberAddrService.queryMemberAddrListByOpenId(openId);
        return Result.success(list);
    }

    @ApiOperation("新增会员收货地址")
    @PostMapping
    public Result<String> saveMemberAddr(@RequestBody MemberAddr memberAddr) {
        String openId = AuthUtils.getLoginUserOpenId();
        memberAddr.setOpenId(openId);
        Boolean saved = memberAddrService.saveMemberAddr(memberAddr);
        return Result.handle(saved);
    }

//    p/address/addrInfo/9
    @ApiOperation("根据标识查询会员收货地址信息")
    @GetMapping("addrInfo/{addrId}")
    public Result<MemberAddr> loadMemberAddrInfo(@PathVariable Long addrId) {
        MemberAddr memberAddr = memberAddrService.getById(addrId);
        return Result.success(memberAddr);
    }

    @ApiOperation("修改会员收货地址信息")
    @PutMapping
    public Result<String> modifyMemberAddr(@RequestBody MemberAddr memberAddr) {
        String openId = AuthUtils.getLoginUserOpenId();
        memberAddr.setOpenId(openId);
        Boolean modified = memberAddrService.modifyMemberAddr(memberAddr);
        return Result.handle(modified);
    }

//    p/address/deleteAddr/10
    @ApiOperation("删除会员收货地址")
    @DeleteMapping("deleteAddr/{addrId}")
    public Result<String> removeMemberAddr(@PathVariable Long addrId) {
        String openId = AuthUtils.getLoginUserOpenId();
        Boolean removed = memberAddrService.removeMemberAddrByAddrId(openId,addrId);
        return Result.handle(removed);
    }

//    p/address/defaultAddr/8
    @ApiOperation("设置新的默认收货地址")
    @PutMapping("defaultAddr/{newAddrId}")
    public Result<String> modifyMemberDefaultAddr(@PathVariable Long newAddrId) {
        String openId = AuthUtils.getLoginUserOpenId();
        Boolean modified = memberAddrService.modifyMemberDefaultAddr(openId,newAddrId);
        return Result.handle(modified);
    }



    ///////////////////////// feign 接口 //////////////////////////////
    @ApiOperation("根据标识查询会员收货地址信息")
    @GetMapping("getMemberAddrById")
    public Result<MemberAddr> getMemberAddrById(@RequestParam Long id) {
        MemberAddr memberAddr = memberAddrService.getById(id);
        return Result.success(memberAddr);
    }

    @GetMapping("getMemberDefaultAddrByOpenId")
    public Result<MemberAddr> getMemberDefaultAddrByOpenId(@RequestParam String openId, @RequestParam Long addrId) {
        // 根据会员openid查询默认收货地址
        LambdaQueryWrapper<MemberAddr> wrapper = new LambdaQueryWrapper<MemberAddr>();
        wrapper.eq(MemberAddr::getOpenId, openId);
        if (addrId != null && addrId != 0) {
            wrapper.eq(MemberAddr::getAddrId, addrId);
        } else {
            wrapper.eq(MemberAddr::getCommonAddr, 1);
        }
        MemberAddr memberAddr = memberAddrService.getOne(wrapper);

        return Result.success(memberAddr);
    }
}
